package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderUtil implements OrderDao {
	private Connection conn;
	private PreparedStatement orderQuery;
	private ResultSet rs;
	@Override
	public List<Order> getOrder() {
		List<Order> orderList=new ArrayList<Order>();
		try {
			conn=DBcon.getConnection();
			String sql="select id,order_id,total,quantity,phone,address from doorder ";
			orderQuery=conn.prepareStatement(sql);
			ResultSet rs=orderQuery.executeQuery();
			while(rs.next()) {
				Order order=new Order();
				order.setId(rs.getInt("id"));
				order.setOrder_id(rs.getString("order_id"));
				order.setTotal(rs.getDouble("total"));
				order.setQuantity(rs.getInt("quantity"));
				order.setPhone(rs.getString("phone"));
				order.setAddress(rs.getString("address"));
				orderList.add(order);
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		return orderList;
	}
		
	@Override
	public int add(Order orderbean) {
		int result=0;
		try {
		conn=DBcon.getConnection();
		String sql="insert into doorder (id,order_id,total,quantity,phone,address) values(null,?,?,?,?,?)";
		orderQuery=conn.prepareStatement(sql);
		orderQuery.setString(1,orderbean.getOrder_id());
		orderQuery.setDouble(2,orderbean.getTotal());
		orderQuery.setInt(3, orderbean.getQuantity());
		orderQuery.setString(4, orderbean.getPhone());
		orderQuery.setString(5,orderbean.getAddress());
		result=orderQuery.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		return result;
		
	}

	@Override
	public int delete(int id) {
		int result=0;
		try {
			conn=DBcon.getConnection();
			String sql="delete from doorder where id='"+id+"'";
			orderQuery=conn.prepareStatement(sql);
			result=orderQuery.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		return result;
	}

	@Override
	public int update(Order orderbean) {
		int result=0;
		try {
			conn=DBcon.getConnection();
			String sql="update doorder set order_id=?,total=?,quantity=?,phone=?,address=?  where id=?";
			orderQuery=conn.prepareStatement(sql);
			orderQuery.setString(1,orderbean.getOrder_id());
			orderQuery.setDouble(2,orderbean.getTotal());
			orderQuery.setInt(3, orderbean.getQuantity());
			orderQuery.setString(4,orderbean.getPhone());
			orderQuery.setString(5,orderbean.getAddress());
			orderQuery.setInt(6, orderbean.getId());
			
			result=orderQuery.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		return result;
	}

	@Override
	public Order findById(int id) {
		Order order=null;
		try {
			conn=DBcon.getConnection();
			String sql="select * from doorder where id='"+id+"'";
			orderQuery=conn.prepareStatement(sql);
			rs=orderQuery.executeQuery();
			if(rs.next()) {
				order=new Order();
				order.setId(rs.getInt("id"));
				order.setOrder_id(rs.getString("order_id"));
				order.setTotal(rs.getDouble("total"));
				order.setQuantity(rs.getInt("quantity"));
				order.setPhone(rs.getString("phone"));
				order.setAddress(rs.getString("address"));
				
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		
		return order;
	}

	@Override
	public Order findByOrder_id(String order_id) {
		Order order=null;
		try {
			conn=DBcon.getConnection();
			String sql="select * from doorder where order_id='"+order_id+"'";
			orderQuery=conn.prepareStatement(sql);
			rs=orderQuery.executeQuery();
			if(rs.next()) {
				order=new Order();
				order.setId(rs.getInt("id"));
				order.setOrder_id(rs.getString("order_id"));
				order.setTotal(rs.getDouble("total"));
				order.setQuantity(rs.getInt("quantity"));
				order.setPhone(rs.getString("phone"));
				order.setAddress(rs.getString("address"));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			DBcon.closeConnection(conn);
			DBcon.closeResult(rs);
			DBcon.closeStatement(orderQuery);
		}
		
		return order;
	}

}
